package com.typy.www.tpay.ui.acitivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.typy.www.tpay.R;
import com.typy.www.tpay.base.BaseBackActivity;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.bean.vm.UserInfoVm;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.constance.Sharep;
import com.typy.www.tpay.ui.dialog.SyDialogHelper;
import com.typy.www.tpay.ui.dialog.SyMessageDialog;
import com.typy.www.tpay.util.CheckUtil;
import com.typy.www.tpay.util.ClassHoldUtil;
import com.typy.www.tpay.util.ClickFastUtil;
import com.typy.www.tpay.util.GsonUtils;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.PreferencesUtil;
import com.typy.www.tpay.util.PwdDailogUtil;
import com.typy.www.tpay.util.UiHelper;
import com.typy.www.tpay.util.UpdataUiUtil;
import com.typy.www.tpay.util.inter.InputCallBack;
import com.typy.www.tpay.util.listviewanimations.IntentUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;
import com.zhy.android.percent.support.PercentLinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 13799 on 2018/6/18.
 */

public class ExchangePointActivity extends BaseBackActivity {

    @BindView(R.id.btn_titlebar_left)
    ImageButton btnTitlebarLeft;
    @BindView(R.id.btn_titlebar_right)
    Button btnTitlebarRight;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_exchangeamount)
    AutoCompleteTextView etExchangeamount;
    @BindView(R.id.querenduihuan)
    TextView querenduihuan;
    @BindView(R.id.et_paypassword)
    AutoCompleteTextView etPaypassword;
    @BindView(R.id.ll_paypassword)
    PercentLinearLayout llPaypassword;
    UserInfoVm userInfoVm;
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.tv_point)
    TextView tvPoint;
    @BindView(R.id.tv_tip)
    TextView tvTip;

    public static void show(Context context) {
        IntentUtil.intent2Activity(context, ExchangePointActivity.class);
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        etPaypassword.setVisibility(View.GONE);
        tvTitle.setText("兑换积分");
        btnTitlebarRight.setText("兑换记录");
        btnTitlebarRight.setVisibility(View.VISIBLE);
        tvTip.setText(PreferencesUtil.getValue(Sharep.EXCHANGETIP));
        getData();
    }

    @Override
    protected int getTitleBarResId() {
        return -1;
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_exchangepoint;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
    }

    @OnClick({R.id.btn_titlebar_left, R.id.btn_titlebar_right, R.id.querenduihuan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_titlebar_left:
                finish();
                break;
            case R.id.btn_titlebar_right:
                RecordActivity.show(getActivity(), ClassHoldUtil.ExchangePointRecord);
                break;
            case R.id.querenduihuan:
                if(ClickFastUtil.isFastClick500()) {
                    if (CheckUtil.checkText(this, etExchangeamount))
                        PwdDailogUtil.show(this, new InputCallBack() {
                            @Override
                            public void onInputFinish(String result) {
                                save(result);
                            }
                        });
                }
                break;
        }
    }

    //获取个人信息
    private void getData() {
        ParamsUtil params = new ParamsUtil();
        HTTPUtils.post(this, Constance.getMyselfinfoURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                userInfoVm = GsonUtils.parseJSON(ret.getData().toString(), UserInfoVm.class);
                updataUserInfo();
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

    private void updataUserInfo() {
        if (userInfoVm != null)
            UpdataUiUtil.getDefaul().setText(userInfoVm.getFdAmount(), tvAmount)
                    .setText(userInfoVm.getFdPoint(), tvPoint);
    }

    private void save(String password) {
        ParamsUtil paramsUtil = new ParamsUtil();
        paramsUtil.put("fdAmount", etExchangeamount);
        paramsUtil.putMd5("fdPayPwd", password);
        HTTPUtils.post(this, Constance.getExchangepointURL(), paramsUtil.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                PwdDailogUtil.hide();
                SyDialogHelper.showDlg(getActivity(), "提示", "兑换成功", "确定", new SyMessageDialog.OnClickListener() {
                    @Override
                    public void onClick(SyMessageDialog dialog) {
                        RecordActivity.show(getActivity(), ClassHoldUtil.ExchangePointRecord);
                        finish();
                    }
                });
            }

            @Override
            public void onResponseErr(int code, String msg) {
                if (!Constance.WRONGPWD.equals(msg))
                    PwdDailogUtil.hide();
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }
}
